﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using AfterlifeLib.Levels;

namespace AfterlifeLib
{
    public class Camera2D
    {
        private static int m_screenWidth = 1280;
        /// <summary>
        /// The width of the screen.
        /// </summary>
        public static int ScreenWidth
        {
            get { return m_screenWidth; }
            set { m_screenWidth = value; }
        }

        private static int m_screenHeight = 720;
        /// <summary>
        /// The height of the screen.
        /// </summary>
        public static int ScreenHeight
        {
            get { return m_screenHeight; }
            set { m_screenHeight = value; }
        }

        private Vector2 cameraPosition;
        /// <summary>
        /// The current position of the camera.
        /// </summary>
        public Vector2 Position
        {
            get { return cameraPosition; }
        }

        /// <summary>
        /// Gets the X coordinate of the camera as an Int32 so when
        /// drawing the layer it doesn't look weird.
        /// </summary>
        public int IntX
        {
            get { return (int)cameraPosition.X; }
            set { cameraPosition.X = value; }
        }

        /// <summary>
        /// Gets the Y coordinate of the camera as an Int32 so when
        /// drawing the layer it doesn't look weird.
        /// </summary>
        public int IntY
        {
            get { return (int)cameraPosition.Y; }
            set { cameraPosition.Y = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="position">The initial position of the camera.</param>
        public Camera2D(Vector2 position)
        {
            this.cameraPosition = position;
        }

        /// <summary>
        /// Method used to set the position.
        /// </summary>
        /// <param name="position">The new position.</param>
        public void SetPosition(Vector2 position)
        {
            this.cameraPosition = position;
        }

        /// <summary>
        /// Method used to translate the camera.
        /// </summary>
        /// <param name="movement">How much to translate.</param>
        public void Translate(Vector2 movement)
        {
            this.cameraPosition += movement;
        }

        /// <summary>
        /// Method used to center the camera on a specified point.
        /// </summary>
        /// <param name="position">The point to center the camera to.</param>
        public void CenterPosition(Vector2 position)
        {
            this.cameraPosition = new Vector2(position.X - (ScreenWidth / 2), position.Y - (ScreenHeight / 2));
        }

        /// <summary>
        /// Method used to make sure the player doesn't see outside the level's bounds.
        /// </summary>
        /// <param name="level">The level.</param>
        public void ClampPosition(Level level)
        {
            cameraPosition.X = MathHelper.Clamp(cameraPosition.X, 0, level.WidthInPixels - ScreenWidth);
            cameraPosition.Y = MathHelper.Clamp(cameraPosition.Y, 0, level.HeightInPixels - ScreenHeight);
        }
    }
}
